const fs=require('fs')

class CDBSave{
    constructor(){
        this.data={};
        this.loadData();
        setInterval(this.saveData,5000,this);
    }

    filePath(){
        return ''
    }

    saveData(self){
        if(!self.filePath())return
        let strData=JSON.stringify(self.save());
        fs.writeFileSync(`./db/${self.filePath()}`,strData)
    }

    loadData(){
        if(!this.filePath()){
            this.load(null);
            return
        }
        let strData=fs.readFileSync(`./db/${this.filePath()}`);
        let data=JSON.parse(strData);
        this.load(data)
    }

    save(){
        if(!this.filePath())return{}
        return this.data;
    }

    load(data){
        this.data=data;
    }
}

module.exports={CDBSave}